package com.example.yimai.controller;

import com.example.yimai.entity.ShopCartItem;
import com.example.yimai.service.impl.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/cart")
public class ShopCartController {

    @Autowired
    private ShopCartService shopCartService;

    @RequestMapping("/get")
    public List<ShopCartItem> getCart(@RequestAttribute("id") String userId){
        return shopCartService.getCart(Long.valueOf(userId));
    }

    @RequestMapping("/add")
    public String addToCart(@RequestAttribute("id") String userId,
                            @RequestBody ShopCartItem item){
        return shopCartService.addToCart(Long.valueOf(userId), item);
    }

    @RequestMapping("/update")
    public String updateQuantity(@RequestAttribute("id") String userId,
                                 @RequestParam String productId,
                                 @RequestParam int quantity){
        return shopCartService.updateQuantity(Long.valueOf(userId), productId, quantity);
    }

    @RequestMapping("/remove")
    public String removeFromCart(@RequestAttribute("id") String userId,
                                 @RequestParam String productId){
        return shopCartService.removeFromCart(Long.valueOf(userId), productId);
    }

    @RequestMapping("/clear")
    public String clearCart(@RequestAttribute("id") String userId){
        return shopCartService.clearCart(Long.valueOf(userId));
    }

}
